﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class Details : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            string SQLString = "SELECT * FROM Product " + "WHERE PD_ID = " + Request.QueryString[0] + "";
            sdsDB.SelectCommand = SQLString;
        }
    }

    protected void btnClick_addToCart(object sender, EventArgs e)
    {
        //Get Labels within fvDisplay
        Label fvPDID = fvDisplay.FindControl("lblPDID") as Label;

        //Cart: PD_ID, CT_ID, CT_QTY
        OleDbCommand DBCommand = new OleDbCommand();
        string SQLstring, SQLAddString;

        OleDbConnection DBConnection = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + MapPath("App_Data\\Database1.accdb"));
        DBConnection.Open();
        
        SQLstring = "SELECT Count(*) FROM CART " + "WHERE CT_ID = " + Session["CT_ID"] + " " + "AND PD_ID = " + fvPDID.Text + "";
        DBCommand = new OleDbCommand(SQLstring, DBConnection);

        string matchstr = DBCommand.ExecuteScalar().ToString();
        int match = int.Parse(matchstr) ;// = int.Parse(DBCommand.ExecuteScalar().ToString());
        if (match == 0)
        {
            SQLAddString = "INSERT INTO CART (CT_ID, CT_QTY, PD_ID) VALUES (" + Session["CT_ID"] + ", 1 , " + fvPDID.Text + ")";
            DBCommand = new OleDbCommand(SQLAddString, DBConnection);
            DBCommand.ExecuteNonQuery();
        }

        DBConnection.Close();
    }
}